package TrabalhoEda;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.JFrame;

/*
 * Class DesenhaPontos
 *  Autor Joao Alturas_n_6197
 *  Data 2014-05-27
 */

class DesenhaPontos {
	private BufferedImage bufferedImage;
	private static final String fname = ("D:\\EDA13_14\\peppersgrad.bmp");
	private ImageIO image;
	private double maxValue = 255;
	private static int startX;
	private static int startY;
	private static int endX;
	private static int endY;
	private static int width = 512;
	private static int height = 512;
	private double[][] pixeis;
	private ArrayList<Double> mediaTempo;

	public void run() throws IOException {

		this.loadImage(this.image);

		this.loadPixel();

		this.apresentaFrame();
	}

	/*
	 * Method constructor of the DesenhaPontos
	 * 
	 * @param int startX
	 * 
	 * @param int startY
	 * 
	 * @param int endX
	 * 
	 * @param int endY
	 */
	@SuppressWarnings({ "static-access" })
	DesenhaPontos(int startX, int startY, int endX, int endY) {
		this.startX = startX;
		this.startY = startY;
		this.endY = endY;
		this.endX = endX;

	}

	/*
	 * Method loadImage of the DesenhaPontos
	 * 
	 * @param ImageIO image
	 */
	public void loadImage(ImageIO image) throws IOException {
		File file = new File(fname);

		bufferedImage = ImageIO.read(file);

		height = bufferedImage.getHeight();

		width = bufferedImage.getWidth();

		System.out.println("Dimensoes da imagem =>" + height + " " + width);

	}

	/*
	 * Metodo que le os loadPixel
	 */
	private void loadPixel() {

		this.pixeis = new double[width][height];

		for (int line = 0; line < height; line++) {

			for (int col = 0; col < width; col++) {
				
				int pix = (bufferedImage.getRGB(col, line));

				int b = pix & 0xFF;

				pixeis[col][line] = 1.0 - ((double) b / maxValue);
			}
		}

		int pix = bufferedImage.getRGB(startX, startY);

		int b = pix & 0xFF;

		System.out.println("inicio " + b + " => " + pixeis[startX][startY]);

		pix = bufferedImage.getRGB(endX, endY);

		b = pix & 0xFF;

		System.out.println("fim " + b + " => " + pixeis[endX][endY]);

	}

	/**
	 * Metodo que apresenta a imagem
	 * 
	 * @throws IOException
	 */

	private void apresentaFrame() throws IOException {
		int space = 40;
		long startTime = System.nanoTime();
		 Desempenho desemp = new Desempenho();
		 desemp.writeLog(fname, mediaTempo );
		AStar astar = new AStar(this.pixeis);
		JFrame frame = new JFrame("TRABALHO DE EDA_13/14");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		frame.setResizable(true);
		frame.setLocationRelativeTo(null);
		ApresentarImage apresentarImage = new ApresentarImage(bufferedImage,
				astar.getlistaNosFechados());
		frame.add(apresentarImage);
		frame.setSize(width + space, height + space);
		frame.setVisible(true);
		long duration = System.nanoTime() - startTime;
		System.out.println("Tempo de execucao = >" + duration / 1.0e9);

	}

}
